<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="100px">
      <el-form-item label="物料名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="物料名称"></el-input>
      </el-form-item>

      <el-form-item label="保质期(月)" prop="period">
        <el-input-number v-model="dataForm.period" :min="1" :max="36" label="保质期(月)"></el-input-number>
        <!--<el-input v-model="dataForm.period" placeholder="保质期"></el-input>-->
      </el-form-item>
      <el-form-item label="产地" prop="origin">
        <el-input v-model="dataForm.origin" placeholder="产地"></el-input>
      </el-form-item>
      <el-form-item label="规格" prop="specId">

        <el-select v-model="dataForm.specId"  placeholder="规格Id">
          <el-option
            v-for="item in specs"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <!--<el-input v-model="dataForm.specId" placeholder="规格Id"></el-input>-->
      </el-form-item>
      <!--<el-form-item label="类型" prop="type">-->
        <!--<el-input v-model="dataForm.type" placeholder="类型"></el-input>-->
      <!--</el-form-item>-->
      <el-form-item label="保存方式" prop="hdType">

        <el-select v-model="dataForm.hdType" placeholder="保存方式">
          <el-option
            v-for="item in hds"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="物料条码" prop="guid">
        <el-input v-model="dataForm.guid" placeholder="物料条码"></el-input>
      </el-form-item>
      <!--<el-form-item label="创建时间" prop="createTime">-->
        <!--<el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="创建人" prop="createUser">-->
        <!--<el-input v-model="dataForm.createUser" placeholder="创建人"></el-input>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="更新时间" prop="updateTime">-->
        <!--<el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="更新人" prop="updateUser">-->
        <!--<el-input v-model="dataForm.updateUser" placeholder="更新人"></el-input>-->
      <!--</el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          pdDate: '',
          period: '',
          origin: '',
          specId: '',
          type: 1,
          hdType: '',
          guid: '',
          // createTime: '',
          // createUser: '',
          // updateTime: '',
          // updateUser: ''
        },
        specs: [],
        hds: [
          {value:1,label:"常规存储"},
          {value:2,label:"低温存储"},
          {value:3,label:"其他"},
          ],
        dataRule: {
          name: [
            {required: true, message: '物料名称不能为空', trigger: 'blur'}
          ],
          pdDate: [
            {required: true, message: '生产日期不能为空', trigger: 'blur'}
          ],
          period: [
            {required: true, message: '保质期不能为空', trigger: 'blur'}
          ],
          origin: [
            {required: true, message: '产地不能为空', trigger: 'blur'}
          ],
          specId: [
            {required: true, message: '规格Id不能为空', trigger: 'blur'}
          ],
          // type: [
          //   {required: true, message: '类型不能为空', trigger: 'blur'}
          // ],
          hdType: [
            {required: true, message: '保存方式不能为空', trigger: 'blur'}
          ],
          guid: [
            {required: true, message: '物料条码不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.listSelectSpec()
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/item/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.item.name
                this.dataForm.pdDate = data.item.pdDate
                this.dataForm.period = data.item.period
                this.dataForm.origin = data.item.origin
                this.dataForm.specId = data.item.specId
                // this.dataForm.type = data.item.type
                this.dataForm.hdType = data.item.hdType
                this.dataForm.guid = data.item.guid
                this.dataForm.createTime = data.item.createTime
                this.dataForm.createUser = data.item.createUser
                this.dataForm.updateTime = data.item.updateTime
                this.dataForm.updateUser = data.item.updateUser
              }
            })
          }
        })
      },
      listSelectSpec () {
        this.$http({
          url: this.$http.adornUrl('/generator/spec/listForSelect')
        }).then(({data})=>{
          this.specs = data.data;
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/item/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'pdDate': this.dataForm.pdDate,
                'period': this.dataForm.period,
                'origin': this.dataForm.origin,
                'specId': this.dataForm.specId,
                // 'type': this.dataForm.type,
                'hdType': this.dataForm.hdType,
                'guid': this.dataForm.guid,
                // 'createTime': this.dataForm.createTime,
                // 'createUser': this.dataForm.createUser,
                // 'updateTime': this.dataForm.updateTime,
                // 'updateUser': this.dataForm.updateUser
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
